using BasicScript.Component;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class RefrigeratorItem : BaseItem
	{
		public enum State
		{
			Close = 0,
			LeftOpen = 1,
			RightOpen = 2
		}

		private Transform animTrans;

		private Transform leftDoor;

		private Transform rightDoor;

		private State state;

		private FrameAnimation leftAnim;

		private FrameAnimation rightAnim;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			leftDoor = model.Find("LeftDoor");
			leftDoor.gameObject.layer = 15;
			rightDoor = model.Find("RightDoor");
			rightDoor.gameObject.layer = 15;
			animTrans = frontMask.Find("Anim");
			animTrans.SetLocalZ(-1E-05f);
			leftAnim = animTrans.Find("LeftAnim").GetComponent<FrameAnimation>();
			rightAnim = animTrans.Find("RightAnim").GetComponent<FrameAnimation>();
			if (data.state != 0)
			{
				SetState((State)data.state);
			}
		}

		private void SetState(State val)
		{
			if (state != val)
			{
				state = val;
				UpdateState();
				if (state == State.LeftOpen)
				{
					leftAnim.gameObject.SetActive(true);
					rightAnim.gameObject.SetActive(false);
					leftAnim.GotoLastFrame();
				}
				else if (state == State.RightOpen)
				{
					leftAnim.gameObject.SetActive(false);
					rightAnim.gameObject.SetActive(true);
					rightAnim.GotoLastFrame();
				}
			}
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (leftAnim.isPlaying || rightAnim.isPlaying)
			{
				return;
			}
			RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 32768);
			if (!(raycastHit2D.collider != null))
			{
				return;
			}
			if (raycastHit2D.collider.transform == leftDoor)
			{
				if (state == State.Close)
				{
					state = State.LeftOpen;
					rightAnim.gameObject.SetActive(false);
					leftAnim.gameObject.SetActive(true);
					leftAnim.Play();
					Singleton<AudioManager>.Instance.PlaySound("sound_refrigerator_open");
				}
				else
				{
					Singleton<AudioManager>.Instance.PlaySound("sound_refrigerator_close");
					CloseDoor();
				}
			}
			else if (raycastHit2D.collider.transform == rightDoor)
			{
				if (state == State.Close)
				{
					state = State.RightOpen;
					leftAnim.gameObject.SetActive(false);
					rightAnim.gameObject.SetActive(true);
					rightAnim.Play();
					Singleton<AudioManager>.Instance.PlaySound("sound_refrigerator_open");
				}
				else
				{
					Singleton<AudioManager>.Instance.PlaySound("sound_refrigerator_close");
					CloseDoor();
				}
			}
			UpdateState();
		}

		private void CloseDoor()
		{
			state = State.Close;
			if (leftAnim.gameObject.activeSelf)
			{
				leftAnim.PlayReversed();
			}
			else
			{
				rightAnim.PlayReversed();
			}
		}

		private void UpdateState()
		{
			if (coll != null)
			{
				BoxCollider2D boxCollider2D = null;
				if (platforms != null && platforms.Count > 0)
				{
					boxCollider2D = platforms[0].GetComponent<BoxCollider2D>();
				}
				if (state == State.Close)
				{
					coll.offset = new Vector2(0f, -0.2f);
					coll.size = new Vector2(5.34f, 1.49f);
					boxCollider2D.enabled = false;
				}
				else if (state == State.LeftOpen)
				{
					coll.offset = new Vector2(1.21f, -0.2f);
					coll.size = new Vector2(2.91f, 1.49f);
					boxCollider2D.offset = new Vector2(-1.12f, -0.2f);
					boxCollider2D.size = coll.size;
					boxCollider2D.enabled = true;
				}
				else if (state == State.RightOpen)
				{
					coll.offset = new Vector2(-1.12f, -0.2f);
					coll.size = new Vector2(2.94f, 1.49f);
					boxCollider2D.offset = new Vector2(1.21f, -0.2f);
					boxCollider2D.size = coll.size;
					boxCollider2D.enabled = true;
				}
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			if (state != 0)
			{
				obj["33"] = (int)state;
			}
		}
	}
}
